package com.musemun.common;

import com.musemun.exception.ApiException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.List;

/**
 * 数据校验
 */
public class DataVerification {
    public static void check(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String data = "";
            for (FieldError i : bindingResult.getFieldErrors()) {
                System.out.println(i.getDefaultMessage());
                data += i.getDefaultMessage();
            }
            throw new ApiException(ResultCode.DATA_ERROR, data);
        }
    }


    public static Long checkNumLong(String num) {
        try{
            return Long.valueOf(num);
        } catch (NumberFormatException e) {
            throw new ApiException(ResultCode.DATA_ERROR, "无法转为Long类型");
        }
    }

    public static int checkNumInteger(String num) {
        try{
            return Integer.valueOf(num);
        } catch (NumberFormatException e) {
            throw new ApiException(ResultCode.DATA_ERROR,  "无法转为Integer类型");
        }
    }
}
